package com.hdphone.zljutils.impl;

import android.annotation.TargetApi;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.Build;
import android.text.TextUtils;

import com.hdphone.zljutils.ZljUtils;
import com.hdphone.zljutils.inter.IAppOpsUtil;

import java.lang.reflect.Method;

/**
 * @date: 2021/6/3
 * @email： yzandlj@163.com
 * @author： yangzhi
 * @describe:
 */
public class AppOpsUtilImpl implements IAppOpsUtil {

    private static final String TAG = "AppOpsUtils";

    /**
     * 是否禁用通知
     */
    @Override
    public boolean allowNotification(Context context) {
        return isAllowed(context, OP_POST_NOTIFICATION);
    }

    /**
     * 是否禁用悬浮窗
     */
    @Override
    public boolean allowFloatWindow(Context context) {
        return isAllowed(context, OP_SYSTEM_ALERT_WINDOW);
    }

    /**
     * 是否禁用某项操作
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    public boolean isAllowed(Context context, int op) {
        ZljUtils.LOG().d(TAG, "api level: " + Build.VERSION.SDK_INT);
        if (Build.VERSION.SDK_INT < 19) {
            return true;
        }
        ZljUtils.LOG().d(TAG, "op is " + op);
        String packageName = context.getApplicationContext().getPackageName();
        AppOpsManager aom = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        Class<?>[] types = new Class[]{int.class, int.class, String.class};
        Object[] args = new Object[]{op, Binder.getCallingUid(), packageName};
        try {
            Method method = aom.getClass().getDeclaredMethod("checkOpNoThrow", types);
            Object mode = method.invoke(aom, args);
            ZljUtils.LOG().d(TAG, "invoke checkOpNoThrow: " + mode);
            if ((mode instanceof Integer) && ((Integer) mode == AppOpsManager.MODE_ALLOWED)) {
                ZljUtils.LOG().d(TAG, "allowed");
                return true;
            }
        } catch (Exception e) {
            ZljUtils.LOG().e(TAG, "invoke error: " + e);
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean hasPermission(Context context, String permission) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return true;
        AppOpsManager opsManager = null;
        int result = context.checkPermission(permission, android.os.Process.myPid(), android.os.Process.myUid());
        if (result == PackageManager.PERMISSION_DENIED) {
            return false;
        }

        String op = AppOpsManager.permissionToOp(permission);
        if (TextUtils.isEmpty(op)) {
            return false;
        }
        if (opsManager == null)
            opsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        result = opsManager.checkOpNoThrow(op, android.os.Process.myUid(), context.getPackageName());
        if (result != AppOpsManager.MODE_ALLOWED && result != 4) {
            return false;
        }
        return true;
    }
}
